package com.slice.upload.autoconfigure;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.slice.upload.base.StrategyAdvisor;
import com.slice.upload.service.FileService;
import com.slice.upload.service.RedisService;
import com.slice.upload.strategy.FileUploadHandler;
import com.slice.upload.strategy.biz.MappedByteBufferUploadStrategy;
import com.slice.upload.strategy.biz.RandomAccessUploadStrategy;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ClassName SliceUploadAutoConfiguration
 * @Description 启动配置类
 * @Author frinder
 * @Date 2021/1/27 20:10
 * @Verion 1.0
 */
@Configuration
@EnableConfigurationProperties(SliceUploadProperties.class)
public class SliceUploadAutoConfiguration {

    @Bean
    @ConditionalOnClass({RedisService.class})
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        /** 替换 mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); */
        mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        jackson2JsonRedisSerializer.setObjectMapper(mapper);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    @ConditionalOnMissingBean
    public StrategyAdvisor StrategyAdvisor() {
        return new StrategyAdvisor();
    }

    @Bean
    @ConditionalOnMissingBean
    public RedisService RedisService() {
        return new RedisService();
    }

    @Bean
    @ConditionalOnMissingBean
    public FileService FileService() {
        return new FileService();
    }

    @Bean
    @ConditionalOnMissingBean
    public RandomAccessUploadStrategy RandomAccessUploadStrategy() {
        return new RandomAccessUploadStrategy();
    }

    @Bean
    @ConditionalOnMissingBean
    public MappedByteBufferUploadStrategy MappedByteBufferUploadStrategy() {
        return new MappedByteBufferUploadStrategy();
    }

    @Bean
    @ConditionalOnMissingBean
    public FileUploadHandler FileUploadHandler() {
        return new FileUploadHandler();
    }

}
